﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace FlyData
{
    public partial class SendSMSDialog : Form
    {
        public SendSMSDialog()
        {
            InitializeComponent();
        }


        private void sendSMSButton_Click(object sender, EventArgs e)
        {
            send("9994398529", "ksudha", "9500089187", "hi");
        }


        void send(string uid, string pwd, string no, string msg)
        {
            String content = "username=" + uid + "&password=" + pwd;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/auth.cl");
                request.KeepAlive = false;
                byte[] byteArray = Encoding.UTF8.GetBytes(content);
                CookieContainer cookies = new CookieContainer();
                request.CookieContainer = cookies;
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
                request.ContentLength = byteArray.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.Referer = "http://wwwg.way2sms.com//entry.jsp";
                request.Method = "POST";
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                foreach (Cookie cook in response.Cookies)
                {
                    cookies.Add(cook);
                }
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                string serverData = reader.ReadToEnd();
                reader.Close();
                content =
                    "HiddenAction=instantsms&login=&pass=&custid=&Action=custfrom1000000&sponserid=undefined&MobNo=" +
                    no + "&textArea=" + msg;
                request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/FirstServletsms?custid=");
                byteArray = Encoding.UTF8.GetBytes(content);
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
                request.ContentLength = byteArray.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.CookieContainer = cookies;
                request.Method = "POST";
                dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                response = (HttpWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                reader = new StreamReader(stream);
                serverData = reader.ReadToEnd();
                reader.Close();
                request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/jsp/logout.jsp");
                byteArray = Encoding.UTF8.GetBytes(content);
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
                request.ContentLength = byteArray.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.CookieContainer = cookies;
                request.Method = "POST";
                dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                response = (HttpWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                reader = new StreamReader(stream);
                serverData = reader.ReadToEnd();
                reader.Close();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("arg exception");
                Console.Read();

            }
            catch (WebException e)
            {
                Console.WriteLine("web exception");
                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine("exception");
                Console.Read();
            }
        }

    }
}

